package cn.tedu.manager.controller;

import cn.tedu.manager.mapper.ManagerMapper;
import cn.tedu.manager.pojo.dto.ManagerDTO;
import cn.tedu.manager.pojo.entity.Manager;
import cn.tedu.manager.pojo.vo.ManagerApiVO;
import cn.tedu.manager.service.ManagerService;
import cn.tedu.manager.utils.JsonResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "管理员外部接口")
@RequestMapping("/v1/api/manager")
public class ManagerApiController {
    @Autowired
    private ManagerService managerService;
    @Autowired
    private ManagerMapper managerMapper;

    @ApiOperation("只包含ID和名字的列表")
    @GetMapping("list")
    public JsonResult list(ManagerDTO managerDTO) {
        LambdaQueryWrapper<Manager> queryWrapper = new LambdaQueryWrapper<>();
        if (managerDTO.getId() != null) {
            queryWrapper.eq(Manager::getId, managerDTO.getId());
        }

        if (managerDTO.getUsername() != null && !managerDTO.getUsername().trim().isEmpty()) {
            queryWrapper.eq(Manager::getUsername, managerDTO.getUsername());
        }

        if (managerDTO.getRoleId() != null) {
            queryWrapper.eq(Manager::getRoleId, managerDTO.getRoleId());
        }
        queryWrapper.isNull(Manager::getDeleteTime);


        List<Manager> list = managerMapper.selectList(queryWrapper);
        List<ManagerApiVO> listApiVO = list.stream()
                .map(manager -> {
                    ManagerApiVO managerVO = new ManagerApiVO();
                    BeanUtils.copyProperties(manager, managerVO);
                    return managerVO;
                })
                .collect(Collectors.toList());
        return JsonResult.ok(listApiVO);
    }
}
